import { env } from "../../../utils/env";
Page({

  /**
   * 页面的初始数据
   */
  data: {
      reson1:'',
      qtreson:'',
      fileList2:[],
      fileList1:[],
      imgnum:'0',
      imgnum2:'0',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },
  afterRead1(event){
    const {  file } = event.detail 
    let that  = this
    console.log(file,'file');
    wx.uploadFile({
      filePath: file.tempFilePath,
      name: 'file',
      url: `${env.baseURL}/sys/common/upload`,
      header:{
        'X-Access-Token':wx.getStorageSync("token") || "",
         'content-type': 'multipart/form-data',
        'accept': 'application/json',
    },
    success:async function(e){
      const imgInfo =  JSON.parse(e.data);
      const arr = []
      arr.push({file,url:imgInfo.message.save,paths:imgInfo.message.savePath})
      that.setData({fileList1:[...arr]})
      that.setData({imgnum:that.data.fileList1.length})
    }
    })
  },
  afterRead2(event){
    const {  file } = event.detail 
    let that  = this
    console.log(file,'file');
    wx.uploadFile({
      filePath: file.tempFilePath,
      name: 'file',
      url: `${env.baseURL}/sys/common/upload`,
      header:{
        'X-Access-Token':wx.getStorageSync("token") || "",
         'content-type': 'multipart/form-data',
        'accept': 'application/json',
    },
    success:async function(e){
      const imgInfo =  JSON.parse(e.data);
      const arr = []
      arr.push({file,url:imgInfo.message.save,paths:imgInfo.message.savePath})
      that.setData({fileList2:[...arr]})
      that.setData({imgnum2:that.data.fileList2.length})
    }
    })
  },
//删除图片
deleteImg(data)
{
  const idx = data.detail.index
   this.data.fileList1.splice(idx,1)
  this.setData({fileList1:this.data.fileList1,imgnum:this.data.fileList1.length})
},
deleteImg2(data)
{
  const idx = data.detail.index
   this.data.fileList2.splice(idx,1)
  this.setData({fileList2:this.data.fileList2,imgnum2:this.data.fileList2.length})
},
  //确定
  passed()
  {
    if(this.data.reson1 == "")
    {
      wx.showToast({
        title: '活动标题不能为空',
        icon:'none'
      })
    }else if(this.data.qtreson == "")
    {
      wx.showToast({
        title: '活动内容不能为空',
        icon:'none'
      })
    }else if(this.data.imgnum == 0)
    {
      wx.showToast({
        title: '封面图片不能为空',
        icon:'none'
      })
    }else
    {
      const params = {
        title:this.data.reson1,
        content:this.data.qtreson,
        img:this.data.fileList1[0].paths,
        img2:this.data.fileList2.length > 0 ? this.data.fileList2[0].paths : '',
     }
     console.log(params,'obj');
     const obj = JSON.stringify(params)
     wx.navigateTo({
      url: `../../../subPackagesB/pages/publish/publish?msg=${obj}`,
    })
  
    }

 
  },
  reject(){
    // wx.navigateTo({
    //   url: '../../../subPackagesB/pages/myapproval/myapproval',
    // })
    wx.navigateBack(-1)
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})